package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.anno.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;


    //--1--新增
    @Override
    @Transactional
    public void insertOne(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //-1--添加菜品，主键返回
        setmealMapper.insertOne(setmeal);
        Long setmealId = setmeal.getId();

        //--注入：套餐Id
        List<SetmealDish> setmealDishList = setmealDTO.getSetmealDishes()
                .stream().map(item -> {
                    item.setSetmealId(setmealId);
                    return item;
                }).collect(Collectors.toList());
        setmealDishMapper.insertBySetmealIdBath(setmealDishList);//批量添加

    }

    //-2--套餐分页
    @Override
    public PageResult selectPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> setmealPage=setmealMapper.selectPage(setmealPageQueryDTO);
        return new PageResult(setmealPage.getTotal(),setmealPage.getResult());
    }
    //-3--删除套餐
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //-1- 停售才能删
        Integer count=setmealMapper.selectStatusById(ids);

        //-2- 删除套餐
        if (count !=null && count>0){
            throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE); //起售不能删
        }
        setmealMapper.deleteByIds(ids);

        //-3- 删除套餐关联
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    //-4.1--根据id查询套餐
    @Override
    public SetmealVO selectById(Long id) {
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmealMapper.selectById(id),setmealVO); //1、etmeal数据
        setmealVO.setCategoryName(categoryMapper.selectById(setmealVO.getCategoryId()).getName());//2、注入CategoryName
        setmealVO.setSetmealDishes(setmealDishMapper.selectBySetmealId(id));//3、塞入包含的菜品（非Dish）
        return setmealVO;
    }
    //-4.2--修改套餐
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //--1--修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateSetmeal(setmeal);

        //--2--清空，套餐菜品关系
        setmealDishMapper.deleteBySetmealId(setmeal.getId());

        //--3--重新加如套餐菜品关系
        List<SetmealDish> setmealDishList=setmealDTO.getSetmealDishes()
                .stream().map(item->{
                    item.setSetmealId(setmeal.getId());
                    return item;
                }).collect(Collectors.toList());
        setmealDishMapper.insertBySetmealIdBath(setmealDishList);
    }

    //-5--套餐起售、停售   状态 0:停用 1:启用
    @Override
    public void updateSetmealByStatus(Integer status, Long id) {
        //--1--启用菜品，判断套餐内菜品是否起售
        if(status==1){
            List<Long> longList=setmealDishMapper.selectBySetmealId(id).stream().map(item->{
                return item.getDishId();
            }).collect(Collectors.toList());
            Integer integer = dishMapper.countByStatusAndIds(longList);//套餐内--起售的菜品的个数
            //套餐内包含未启售菜品，无法启售
            if(integer!=null && integer!=longList.size()){
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        //--2--套餐起售、停售
        setmealMapper.updateSetmealByStatus(status,id);
    }

    //-4.1--根据分类id查询套餐   /user/setmeal/list
    @Override
    public List<Setmeal> selectByCategoryId(Long categoryId) {
        List<Setmeal> setmealList=setmealMapper.selectListByCategoryID(categoryId);
        return setmealList;
    }


    //-4.1--【用户端】根据套餐id查询菜品
    @Override
    public List<DishItemVO> selectDishItemVOBySetmealId(Long id) {
        List<SetmealDish> setmealDishList=setmealDishMapper.selectBySetmealId(id);

        List<DishItemVO> dishItemVOList=setmealDishList.stream().map(item->{
            DishItemVO dishItemVO=new DishItemVO();
            Dish dish=dishMapper.selectById(item.getDishId());

            //菜名name  份数copies 菜品图片image 菜品描述description
            dishItemVO.setName(item.getName());
            dishItemVO.setCopies(item.getCopies());
            dishItemVO.setImage(dish.getImage());
            dishItemVO.setDescription(dish.getDescription());
            return dishItemVO;
        }).collect(Collectors.toList());
        return dishItemVOList;
    }
}
